package rezetkaPages;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import utils.ReportWriter;

import java.util.concurrent.TimeUnit;

/**
 * Created by IIGalytska on 10.04.14.
 */
public class HomePage extends BasePage{

    public HomePage(WebDriver driver) {
        super(driver);
    }

    public ResultPage search(String text){
        if (driver.findElement(searchBar).isDisplayed()){
            ReportWriter.info("SearchBar is found");
        }else {
            ReportWriter.error("Could not find search bar");
        }

        driver.findElement(searchBar).sendKeys(text);
        ReportWriter.info("Search for the following item " + text);
        driver.findElement(saerchButton).click();


        return new ResultPage(driver);

    }

    public  NavigationPage navigateTo(int menu_page_number){



        switch (menu_page_number){
            case 1:
                driver.findElement(computersNotebooks).click();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                break;
            case 2:
                driver.findElement(phonesMp3Gps).click();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                break;
            case 3:
                driver.findElement(tvPhotoVideo).click();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                break;
            case 4:
                driver.findElement(householdAppliances).click();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                break;
            case 5:
                driver.findElement(outdoorsMan).click();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                break;
            case 6:
                driver.findElement(kids).click();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                break;
            case 7:
                driver.findElement(houseGarden).click();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                break;
            case 8:
                driver.findElement(avto).click();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                break;
            case 9:
                driver.findElement(airTrainTickets).click();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                break;

            default:
                driver.findElement(mainPage).click();
                break;
        }
        return new  NavigationPage(driver);
    }

    public NavigationPage authorization(String email, String password){

        driver.findElement(By.name("signin")).click();

        driver.findElement(By.name("login"));
        driver.findElement(By.name("login")).clear();
        driver.findElement(By.name("login")).sendKeys(email);
        //Assert.assertEquals(email, driver.findElement(By.name("login")).getAttribute("value"));
        ReportWriter.info("Authorization with the following login: " + email);
        driver.findElement(By.name("password"));
        driver.findElement(By.name("password")).clear();
        driver.findElement(By.name("password")).sendKeys(password);
        ReportWriter.info("Authorization with the following password: " + password);
        driver.findElement(By.name("password")).sendKeys(Keys.ENTER);

        //driver.findElement(By.xpath("//*[@id=\"user-popup-parent95944\"]/div/div/div/form/div[4]/button")).click();

        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.elementToBeClickable(By.name("profile")));
        driver.findElement(By.name("profile")).click();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        return new NavigationPage(driver);
    }

    public NavigationPage authorizationNegative(String email, String password){
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.elementToBeClickable(By.name("signin")));
        driver.findElement(By.name("signin")).click();
        driver.findElement(By.name("login"));
        driver.findElement(By.name("login")).clear();
        driver.findElement(By.name("login")).sendKeys(email);
        ReportWriter.info("Authorization with the following login: " + email);
        //Assert.assertEquals(email, inputElement1.getAttribute("value"));
        driver.findElement(By.name("password"));
        driver.findElement(By.name("password")).clear();
        driver.findElement(By.name("password")).sendKeys(password);
        ReportWriter.info("Authorization with the following INVALID password: " + password);
        driver.findElement(By.name("password")).sendKeys(Keys.ENTER);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        return new NavigationPage(driver);
    }























}
